package cn.tedu.loding.controller;


import cn.tedu.loding.pojo.dto.AlbumUpdateDTO;
import cn.tedu.loding.pojo.dto.PictureAddNewDTO;
import cn.tedu.loding.pojo.dto.PictureUpdateDTO;
import cn.tedu.loding.pojo.vo.AlbumStandardVO;
import cn.tedu.loding.pojo.vo.BrandListItemVO;
import cn.tedu.loding.pojo.vo.PictureListItemVO;
import cn.tedu.loding.pojo.vo.PictureStandardVO;
import cn.tedu.loding.service.IPictureService;
import cn.tedu.loding.web.JsonResult;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理图片相关请求的控制器
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/pictures")
@Api(tags = "06. 图片管理模块")
public class PictureController {

    @Autowired
    private IPictureService pictureService;

    public PictureController() {
        log.debug("创建控制器对象：PictureController");
    }

    @ApiOperation("添加图片")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult addNew(@Valid PictureAddNewDTO pictureAddNewDTO) {
        log.debug("开始处理【添加图片】的请求，参数：{}", pictureAddNewDTO);
        pictureService.addNew(pictureAddNewDTO);
        log.debug("添加图片成功！");
        return JsonResult.ok();
    }

    // http://localhost:8080/albums/9527/delete
    @ApiOperation("根据id删除图片")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "图片id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult delete(@Range(min = 1, message = "删除图片失败，尝试删除的图片的ID无效！")
                             @PathVariable Long id) {
        log.debug("开始处理【根据id删除图片】的请求，参数：{}", id);
        pictureService.delete(id);
        return JsonResult.ok();
    }


    @ApiOperation("修改图片详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "图片id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> update(@PathVariable Long id, PictureUpdateDTO pictureUpdateDTO) {
        log.debug("开始处理【修改图片详情】的请求：id={}, pictureUpdateDTO={}",id,pictureUpdateDTO);
        pictureService.update(id,pictureUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("根据id查询图片详情")
    @ApiOperationSupport(order = 400)
    @ApiImplicitParam(name = "id", value = "图片id", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<PictureStandardVO> getStandardById(@PathVariable Long id) {
        log.debug("开始处理【根据id查询相册详情】的请求，参数：{}", id);
        PictureStandardVO standardById = pictureService.getStandardById(id);
        return JsonResult.ok(standardById);
    }



    @ApiOperation("分页查询的图片列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("/{page}/page/{pageSize}/pageSize")
    public JsonResult<PageInfo<PictureListItemVO>> List(@PathVariable Integer page, @PathVariable Integer pageSize){
        log.debug("开始处理【查询图片列表】请求,参数为{},{}",page,pageSize);
        PageInfo<PictureListItemVO>pageInfo =
                pictureService.getAllPictureByPage(page,pageSize);
        return JsonResult.ok(pageInfo);
    }
}
